<?php

namespace app\common\model;

use think\Exception;
use think\facade\Log;

class Base extends TimeModel
{
    /**
     * @param int $id
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseById(int $id = 0, string $field = '*')
    {
        try {
            $result = $this->field($field)
                ->where('id', $id)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseByWhereOne(array $where = [], string $field = '*')
    {
        try{
            $result = $this->field($field)
                ->where($where)
                ->find();
        }catch (\Exception $e){
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @return array
     */
    public function getBaseByWhere(array $where = [], string $field = '*',string $order='id desc'):array
    {
        try{
            $result = $this->field($field)
                ->where($where)
                ->order($order)
                ->select();
        }catch (\Exception $e){
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @return array
     */
    public function getBaseByWhereCount(array $where = [], string $field = '*')
    {
        try{
            $result = $this->field($field)
                ->where($where)
                ->count();
        }catch (\Exception $e){
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }

        return $result;

    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByList(array $where = [], string $field = '*', string $orderBy = "id desc")
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->select();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 分页方法
     * @param array $where
     * @param int $page
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseBylistPage(array $where =[],int $page =1,int $limit =10,string $field='*',string $orderBy ='id desc'){
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->page($page,$limit)
                ->select();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }
}